package com.dys.sessiondomain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 疾风
 * @ClassName: Servlet02
 * @Description: Session域
 * @CreateDate: 2024/09/23
 * @Version: v1.0.0
 */
@WebServlet(urlPatterns = "/readFromSession.do")
public class Servlet02 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行Servlet02的service方法......");
        // 移除请求域中的数据：在Servlet01中与Servlet02中移除都可以
        // req.removeAttribute("sex");

        // 从Session域中读取数据
        HttpSession session = req.getSession();
        List<String> list = (List<String>) session.getAttribute("list");
        System.out.println(list);
        System.out.println(session.getAttribute("sex"));
        System.out.println(session.getAttribute("name"));

        // 获取Session中的请求参数: 不同的请求域
        System.out.println(req.getParameter("username"));
        System.out.println(req.getParameter("password"));

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("获取Session域中的数据成功");
    }
}
